package com.neuedu.web;

import com.neuedu.pojo.Article;
import com.neuedu.pojo.Userinfo;
import com.neuedu.service.ArticleService;
import com.neuedu.service.ArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "ArticleAddServlet",urlPatterns = "/article/add")
public class ArticleAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String typeid = request.getParameter("class"); // 文章的类型id
        String title = request.getParameter("title");
        String mdcontent = request.getParameter("topic_markdown_content");
        String hmcontent = request.getParameter("test-editormd-html-code");
        String paykiss = request.getParameter("experience");
        int uid = ((Userinfo)request.getSession().getAttribute("login_user")).getUid();
        Date releasetime = new Date();
        Article article = new Article();
        article.setTypeid(Integer.parseInt(typeid));
        article.setTitle(title);
        article.setMdcontent(mdcontent);
        article.setHmcontent(hmcontent);
        article.setPaykiss(Integer.parseInt(paykiss));
        article.setUid(uid);
        article.setReleasetime(releasetime);
        // 调service层完成新增文章
        ArticleService articleService = new ArticleServiceImpl();
        int num = articleService.releaseArticle(article);
        if(num != 0){
            response.sendRedirect(request.getContextPath()+"/go/index");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
